package com.shop.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.entity.dto.AdminOrders.AdminOrdersListDto;
import com.shop.entity.dto.AdminOrders.AdminOrdersRemoveDto;
import com.shop.entity.dto.AdminOrders.AdminOrdersRestoreDto;
import com.shop.entity.dto.AdminOrders.AdminOrdersStatUpdateDto;
import com.shop.entity.pojo.AdminUser;
import com.shop.entity.pojo.Orders;
import com.shop.entity.pojo.OrdersDetail;
import com.shop.admin.mapper.AdminUserMapper;
import com.shop.admin.mapper.OrdersDetailMapper;
import com.shop.admin.mapper.OrdersMapper;
import com.shop.admin.service.OrdersService;
import com.shop.utils.common.enums.Code;
import com.shop.utils.common.enums.Message;
import com.shop.utils.common.result.PageResponseResult;
import com.shop.utils.common.result.Result;
import com.shop.utils.config.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service

public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService{

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private OrdersDetailMapper ordersDetailMapper;
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result getAdminOrdersList(AdminOrdersListDto dto) {
        dto.checkParam();
        Page<Orders> page = new Page(dto.getPage(), dto.getSize());
        Page<Orders> value = ordersMapper.getOrderByPage(page, dto);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) value.getTotal());
        result.setData(value.getRecords());
        return result;

    }

    @Override
    public Result delOneOrders(AdminOrdersRemoveDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long adminId = BaseContext.getCurrentId();
        AdminUser adminUser = adminUserMapper.selectById(adminId);
        if (adminUser==null||adminId==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Orders orders=new Orders();
        Orders getOrders = ordersMapper.selectOne(Wrappers.<Orders>lambdaQuery().eq(Orders::getId, dto.getOrdersId()));
        if (getOrders==null) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        String k=Message.WeChat_Key_User_Orders+getOrders.getUserId();
        redisTemplate.delete(k);
        OrdersDetail ordersDetail=new OrdersDetail();
        orders= orders.builder()
                .removeAdmin(adminUser.getName())
                .removeMemo(dto.getRemoveMemo()).build();
        ordersDetail= ordersDetail.builder()
                .removeAdmin(adminUser.getName())
                .removeMemo(dto.getRemoveMemo()).build();
        int updateByOrders = ordersMapper.update(orders, Wrappers.<Orders>lambdaUpdate().eq(Orders::getId, getOrders.getId()));
        int updateByOrdersDetail = ordersDetailMapper.update(ordersDetail, Wrappers.<OrdersDetail>lambdaUpdate()
                .eq(OrdersDetail::getOrdersId, getOrders.getOrdersId()));
        if (updateByOrders==Message.Check_Int_False||updateByOrdersDetail==Message.Check_Int_False) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("orders_id",getOrders.getOrdersId());
        int deleteOrders = ordersMapper.deleteById(dto.getOrdersId());
        int deleteOrdersDetail = ordersDetailMapper.deleteByMap(map);
        if (deleteOrders==Message.Check_Int_False||deleteOrdersDetail==Message.Check_Int_False) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        redisTemplate.delete(k);
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result restoreOrders(AdminOrdersRestoreDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long adminId = BaseContext.getCurrentId();
        AdminUser adminUser = adminUserMapper.selectById(adminId);
        Orders byId = ordersMapper.getOne(dto.getOrdersId());
        if (adminId==null||adminUser==null||byId==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        if (byId.getIsDelete()==Message.Delete_True) {
            return Result.errorResult(Code.PARAM_INVALID);
        }
        String k=Message.WeChat_Key_User_Orders+byId.getUserId();
        redisTemplate.delete(k);
        Orders orders=new Orders();
        OrdersDetail ordersDetail=new OrdersDetail();
        orders= orders.builder()
                .id(dto.getOrdersId())
                .removeAdmin(null)
                .removeMemo(null)
                .isDelete(Message.Delete_True)
                .build();
        boolean update = ordersMapper.updateIsDelete(orders);
        if (!update) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        ordersDetail= ordersDetail.builder()
                .ordersId(byId.getOrdersId())
                .removeAdmin(null)
                .removeMemo(null)
                .isDelete(Message.Put)
                .build();
        boolean updateOrders = ordersDetailMapper.updateIsDelete(ordersDetail);
        if (!updateOrders) {
            return Result.errorResult(Code.SERVER_ERROR);
        }
        redisTemplate.delete(k);
        return Result.okResult(Code.SUCCESS);
    }

    @Override
    public Result updateOrdersState(AdminOrdersStatUpdateDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Long adminId = BaseContext.getCurrentId();
        if (adminId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Orders checkOrders = ordersMapper.selectById(dto.getOrdersId());
        Orders orders=new Orders();
        if (checkOrders.getOrdersMode()==Message.WeChat_Orders_Choose_Himself
                &&checkOrders.getOrdersState()==Message.Orders_State_Picking) {
            //需要自提且正在拣货
            orders= orders.builder()
                    .id(checkOrders.getId())
                    .ordersState(Message.Orders_State_Finish).build();
        }else if (checkOrders.getOrdersMode()==Message.WeChat_Orders_Choose_Takeaway
                &&checkOrders.getOrdersState()==Message.Orders_State_Picking) {
            //需要配送且正在拣货
            orders= orders.builder()
                    .id(checkOrders.getId())
                    .ordersState(Message.Orders_State_Distribution).build();
        }else if (checkOrders.getOrdersMode()==Message.WeChat_Orders_Choose_Takeaway
                &&checkOrders.getOrdersState()==Message.Orders_State_Distribution) {
            //需要配送且正在配送
            orders= orders.builder()
                    .id(checkOrders.getId())
                    .ordersState(Message.Orders_State_Finish).build();
        }else {
            return Result.errorResult(Code.PARAM_INVALID);
        }
        ordersMapper.updateById(orders);
        return Result.okResult(Code.SUCCESS);
    }
}




